package com.vishop.controllers;

import java.util.Collection;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.vishop.models.Bestelling;
import com.vishop.models.Winkelwagen;
import com.vishop.services.BestellingService;
import com.vishop.services.PersoonService;
import com.vishop.services.ProductService;

@Controller
public class BestellingController
{
	@Autowired
	private BestellingService bestellingService;
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private PersoonService persoonService;
	
	@RequestMapping("/viewAllBestelling")
	public ModelAndView getAllBestelling(){
		ModelAndView mav = new ModelAndView("showBestelling");
		Collection<Bestelling> bestellingen = bestellingService.getAllBestelling();
		return mav;
	}
	
	@RequestMapping("/viewAllBestellingPersoon")
	public ModelAndView getAllBestellingByPersoon(){
		ModelAndView mav = new ModelAndView("showBestelling");
		Collection<Bestelling> bestellingen = bestellingService.getAllBestellingByPersoon();
		return mav;
	}
	
	@RequestMapping(value="/saveBestelling", method=RequestMethod.GET)
	public ModelAndView newuserForm(){
		ModelAndView mav = new ModelAndView("newBestelling");
		Bestelling bestelling = new Bestelling();
		mav.getModelMap().put("newBestelling", bestelling);
		return mav;
	}
	
	@RequestMapping(value="/besteld", method=RequestMethod.GET)
	public ModelAndView showBesteld(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("besteld");
		
		HttpSession session = request.getSession();
		if(session.getAttribute("winkelwagen") != null) 
		{
			Winkelwagen winkelwagen = (Winkelwagen) session.getAttribute("winkelwagen");
			mav.addObject("tempwinkelwagen", winkelwagen);
			
			session.setAttribute("winkelwagen", new Winkelwagen());	
		}
		
		return mav;
	}
	
	@RequestMapping(value="/bestelling", method=RequestMethod.GET)
	public ModelAndView showBestelling(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("bestelling");
		
		return mav;
	}
	
	@RequestMapping(value="/bestelling", method=RequestMethod.POST)
	public String bestel(HttpServletRequest request){
		
		String redirect = "redirect:besteld.htm";
		
		if(request.getSession().getAttribute("winkelwagen") != null)
		{
			Winkelwagen winkelwagen = (Winkelwagen)request.getSession().getAttribute("winkelwagen");
			
			Bestelling bestelling = new Bestelling();
			
			bestelling.setPersoon(persoonService.getById(1));
			
			bestelling.setDatum(new Date());

			bestelling.setProduct(winkelwagen.getProducten());
			
			bestelling.setTotaalPrijs(winkelwagen.getPrijs());
				
			bestellingService.save(bestelling);
				
			System.out.println("BESTELD!");
		} else { redirect = "redirect:bestelling.htm";}
		
		return redirect;
	}
}
